#!/usr/bin/env python
#
# Copyright 2011 Justin Bruce Van Horne <justinvh@gmail.com>
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at #
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Handles the levels (aka rendering batches) for the editor.

 BatchBrowser: Corresponds to the levels that denote height, but also
               hints for rendering batches in game.
"""

import pyglet

from pyglet.image import load as load_image
from pyglet.sprite import Sprite
from pyglet.gl import *

from shared import *

CHECKED = '%s/%s' % (EDITOR_RES_PATH, 'checked.png')
UNCHECKED = '%s/%s' % (EDITOR_RES_PATH, 'unchecked.png')


class BatchBrowser(pyglet.window.Window):
    """BatchBrowser handles the logic for rendering a batch of tiles."""
    batch = pyglet.graphics.Batch()
    map_editor = None

    def __init__(self):
        super(BatchBrowser, self).__init__(100, 300)
        self.label = pyglet.text.Label('Levels', font_size=12, x=20, y=280)
        self.bullet_unchecked = load_image(UNCHECKED)
        self.bullet_checked = load_image(CHECKED)

        lconf = {
            'y_off': 260,
            'height': 1
        }

        # A quick way to create labels
        def label(lconf):
            height = lconf['height']
            y_off = lconf['y_off']
            l = pyglet.text.Label('Level %s' % height,
                                    font_size=12, x=25, y=y_off)
            lconf['y_off'] -= 25
            lconf['height'] += 1
            return l

        # A quick way to create bullets
        def bullets(lconf, batch):
            y_off = lconf['y_off']
            sprite = Sprite(self.bullet_checked, x=5, y=y_off, batch=batch)
            lconf['y_off'] -= 25
            return sprite

        # Create our batches
        self.batches = [label(lconf) for i in range(10)]
        lconf['y_off'] = 260
        self.bullets = [bullets(lconf, self.batch) for i in range(10)]
        self.checked = [True for i in range(10)]

    def set_map_editor(self, editor):
        """Sets the appropriate map editor"""
        self.map_editor = editor

    def on_draw(self):
        """Handles the logic for drawing"""
        glClearColor(0.6, 0.6, 0.6, 1.0)
        self.clear()
        for batch in self.batches:
            batch.draw()
        self.batch.draw()

    def on_mouse_press(self, x, y, button, modifiers):
        """Handles the renderer updates to the map."""
        for i, bullet in enumerate(self.bullets):
            if (x > bullet.x and y > bullet.y and
                x < (bullet.width + bullet.x) and
                y < (bullet.height + bullet.y)):
                if self.checked[i] is True:
                    bullet.image = self.bullet_unchecked
                else:
                    bullet.image = self.bullet_checked
                self.checked[i] = not self.checked[i]
                if self.map_editor is not None:
                    self.map_editor.update_batches(self.checked)
